// goods/pages/afterSales/afterSales.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reason: '',
    describe: '',
    http: api.http,
    imgList: [],
    order_id: '',
    type: null
  },
  //提交原因
  OnReason() {
    const that = this;
    let itemList = ['订单不能按预计时间送达', '操作有误（商品、地址等选错）', '重复下单/误下单', '其他渠道价格更低', '该商品降价了', '不想买了']
    wx.showActionSheet({
      alertText: '提交原因',
      itemList: itemList,
      success(res) {
        that.setData({
          reason: itemList[res.tapIndex],
        })
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  // 问题描述
  getProblems(e) {
    const that = this;
    that.setData({
      describe: e.detail.value
    })
  },
  //上传图片
  uploadingImg() {
    const that = this;
    if (that.data.imgList.length >= 3) {
      wx.showToast({
        title: '图片限制3张',
        icon: 'error',
        duration: 2000
      })
    } else {
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths[0]
          wx.showLoading({
            title: '上传中...',
          })
          wx.uploadFile({
            filePath: tempFilePaths,
            name: 'head_pic',
            url: api.reqUrls.uploadHeadPic,
            method: "post",
            success(porp) {
              wx.hideLoading();
              let imgUrl = JSON.parse(porp.data)
              if (imgUrl.status == 1) {
                let imgList = that.data.imgList;
                imgList.push(imgUrl.result)
                that.setData({
                  imgList: imgList,
                })
              } else {
                wx.showModal({
                  showCancel: false,
                  content: imgUrl.msg,
                  success(res) {
                    if (res.confirm) {

                    }
                  }
                })
              }


            },
            fail(err) {
              wx.hideLoading();
              wx.showToast({
                title: '图片上传失败',
                icon: 'error',
                duration: 2000
              })
            }
          })
        }
      })
    }

  },
  //删除图片
  remImg(e) {
    const that = this;
    let imgList = that.data.imgList;
    let index = e.currentTarget.dataset.index;
    imgList.splice(index, 1)
    that.setData({
      imgList: imgList,
    })
  },
  addAfterSale() {
    const that = this;
    if (that.data.reason == '') {
      wx.showToast({
        title: '请选择提交原因',
        icon: 'none',
        duration: 2000
      })
    } else {
      let data = {
        order_id: that.data.order_id,
        reason: that.data.reason,
        describe: that.data.describe,
        imgs: that.data.imgList
      }
      wx.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.post(api.reqUrls.addReturnOrder, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
          let pages = getCurrentPages(); //获取小程序页面栈
          let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
          beforePage.getData(); //触发上个页面自定义的shuaxin方法
          // 判断是从订单列表页进入还订单详情进入
          if (that.data.type == 1) {
            let beforePages = pages[pages.length - 3]; //获取上个页面的实例对象
            beforePages.getData(); //触发上个页面自定义的shuaxin方法
          }
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
            confirmText: '我知道了',
            confirmColor: '#ff5151',
            success(e) {
              if (e.confirm) {

              }
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.setData({
      order_id: options.order_id,
      type: options.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})